package com.woniuxy.propertyjava.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@Configuration
public class SpringWebMvcConfig implements WebMvcConfigurer {

    @Value("${upload.resourceHandler}")
    private String resourceHandler;

    @Value("${upload.resourceLocations}")
    private String resourceLocations;

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            //全局跨域统一配置
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")    //运行跨域
                        .allowCredentials(true) //运行携带凭证，Cookie
                        .allowedMethods("*")   //允许跨域的请求类型 post get put delete
                        .allowedHeaders("*")    //允许传递请求头
                        .maxAge(3600);          //允许跨域的时间
            }
        };
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //配置虚拟路径
        registry.addResourceHandler(resourceHandler)
                .addResourceLocations("file:///"+resourceLocations);
    }
}
